package com.cf.studiomanage.controller;

import com.cf.studiomanage.entity.Announcement;
import com.cf.studiomanage.service.AnnouncementService;
import com.cf.studiomanage.util.R;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/announcements")
@Tag(name = "公告模块", description = "系统公告发布与查看接口")
public class AnnouncementController {

    @Autowired
    private AnnouncementService service;

    @PostMapping("/publish")
    @Operation(summary = "发布公告", description = "管理员发布新公告")
    public R publish(@RequestBody Announcement announcement) {
        return service.publish(announcement) ? R.ok("发布成功") : R.error("发布失败");
    }

    @GetMapping("/list")
    @Operation(summary = "获取公告列表", description = "查看所有公告（按时间倒序）")
    public R list() {
        return R.ok().data("list", service.getAll());
    }

    @GetMapping("/{id}")
    @Operation(summary = "公告详情", description = "根据 ID 查看公告内容")
    public R detail(@PathVariable Long id) {
        return R.ok().data("announcement", service.getById(id));
    }
}
